package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.axis.utils.SessionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.AccessInfoDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.dto.XcxNewsDTO;
import com.financing.platform.dto.XcxSearchnoRecordDTO;
import com.financing.platform.service.IXcxNewsService;
import com.financing.platform.service.IXcxSearchnoRecordService;
import com.financing.platform.util.DTOdateSet;


@Controller("xcxNewsController")
@RequestMapping("/api/xcxNews")
public class XcxNewsController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IXcxNewsService xcxNewsService;
	
	
	@Autowired
	private IXcxSearchnoRecordService xcxSearchnoRecordService;
	
	@RequestMapping("/listXcxNews")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, XcxNewsDTO xcxNews,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query xcxNews list begin .......");
			
//			xcxNews.setBegin(pager.getStartIndex());
//			xcxNews.setPageSize(pager.getPageSize());
			int count = xcxNewsService.queryListCount(xcxNews);
			pager.setTotalCount(count);
			List<XcxNewsDTO> xcxNewsList = null;
			
			if(count > 0){
				xcxNewsList = xcxNewsService.queryList(xcxNews);
			}else{
				xcxNewsList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", xcxNewsList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query xcxNews list end .......");
		}catch(Exception e){
			log.error("query xcxNews error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addXcxNews")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, XcxNewsDTO xcxNews){
		ResultMap result = new ResultMap();
		
//		boolean isAccess = false;
		
		try{
			logger.info("add xcxNews  begin .......");
			
//			UserInfoDTO user = (UserInfoDTO) request.getSession().getAttribute(DTOdateSet.user);
//			
//			if ("admin".equals(user.getUsername())) {
//				isAccess = true;
//			}else {
//				List<AccessInfoDTO> accessInfoDTOs = user.getAccessList();
//				for (AccessInfoDTO accessInfoDTO : accessInfoDTOs) {
//					
//					System.out.println("权限："+accessInfoDTO.getActionName());
//					
//					if (accessInfoDTO.getActionName().equals("addXcxNews")) {
//						isAccess = true;
//					};
//				}
//			}
			
//			if (isAccess) {
				DTOdateSet.setCreateInfo(xcxNews, request.getSession());
				DTOdateSet.setUpdateInfo(xcxNews, request.getSession());
				
				XcxSearchnoRecordDTO xcxSearchnoRecord = new XcxSearchnoRecordDTO();
				xcxSearchnoRecord.setFeedbackStatus("0");//未引用
				xcxSearchnoRecord.setKeyword(xcxNews.getKeyWord());
				List<XcxSearchnoRecordDTO> xcxSearchnoRecordlist = xcxSearchnoRecordService.queryList(xcxSearchnoRecord);
				
				//写入消息，将关键词列表此条消息置已读
				xcxSearchnoRecord.setFeedbackStatus("2");
				xcxSearchnoRecordService.updateByKeyword(xcxSearchnoRecord);
				
				for(XcxSearchnoRecordDTO xs: xcxSearchnoRecordlist) {
					//推送消息给用户
					xcxNews.setOpenid(xs.getOpenid());
					xcxNews.setWxUserhearimg(xs.getWxUserhearimg());
					xcxNews.setWxUsername(xs.getWxUsername());
					xcxNews.setStatus("0");//阅读状态-未读

					DTOdateSet.setCreateInfo(xcxNews, request.getSession());
					DTOdateSet.setUpdateInfo(xcxNews, request.getSession());

					xcxNewsService.insert(xcxNews);
				}
				
//				result.setAttribute("isAccess", "true");
				result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
				result.setStatus(ResultMap.STATUS.SUCCESS);
//			}else {
//				
//				//无权限走这里
//				result.setAttribute("isAccess", "false");
//				result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
//				result.setStatus(ResultMap.STATUS.SUCCESS);
//			}
			
			
			logger.info("query xcxNews list end .......");
		}catch(Exception e){
			log.error("query xcxNews error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
